package com.rc.saas.tenant.controller.order;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.order.OrderDetail;
import com.rc.saas.tenant.model.order.OrderDetailExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.order.OrderDetailSearch;
import com.rc.saas.tenant.service.order.OrderDetailService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.order.OrderDetailPrintVo;
import com.rc.saas.tenant.vo.order.OrderDetailResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author frog
 * @date 2019/10/10
 */
@Controller
@RequestMapping("/orderDetail")
public class OrderDetailController extends TenantBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderDetailService orderDetailService;


    /**
     * 跳转订单页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String orderDetailPage(Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        model.addAttribute("user", user);
        return "/templates/order/orderDetail/orderDetail";
    }

    /**
     * 返回订单列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listOrderDetail", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listOrderDetail(OrderDetailSearch orderDetailSearch) {
        TenantUser user = ShiroUtils.getSessionUser();
        orderDetailSearch.setLoginName(user.getLoginName());
        PageInfo<OrderDetailResult> pageInfo = orderDetailService.searchByCondition(orderDetailSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 返回订单时间分段明细
     *
     * @return
     */
    @RequestMapping(value = "/getDetailByOrderNum", method = RequestMethod.GET)
    @ResponseBody
    public List<OrderDetail> getDetailByOrderNum(String outOrderNum) {
        OrderDetailExample orderDetailExample = new OrderDetailExample();
        orderDetailExample.createCriteria().andOutOrderNumEqualTo(outOrderNum);
        return orderDetailService.selectByExample(orderDetailExample);
    }


    /**
     * 导出分段列表
     */
    @ResponseBody
    @RequestMapping("/exportOrderDetail")
    public void exportOrderDetail(OrderDetailSearch orderDetailSearch, HttpServletResponse response) throws Exception {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(orderDetailSearch);
            PageInfo<OrderDetailResult> pageInfo = orderDetailService.searchByCondition(orderDetailSearch);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("订单列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), OrderDetailResult.class).sheet("分段订单列表").doWrite(pageInfo.getList());
        } catch (Exception e) {
            logger.error("导出分段列表失败", e.getMessage());
        }
    }

}
